package xyz.sicau.blog.controller;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.sicau.blog.dao.UserMapper;
import xyz.sicau.blog.utils.JwtUtils;
import xyz.sicau.blog.utils.UserThreadLocal;

import javax.annotation.Resource;

/**
 * 用来装需要 Token 验证的接口
 */
@RestController
@Api(tags = "用户信息编辑接口")
@RequestMapping("/user/set")
public class UserSetController {
    @Resource
    UserMapper userMapper;

    /**
     * 修改用户信息
     */
    // 用户名
    @RequestMapping("/name")
    int setName(String usertoken, String value) {
        //int userId = JwtUtils.getUserIdByToken(usertoken);
        int userId = Integer.parseInt(UserThreadLocal.get().get("userId"));
        System.out.println("/usertoken/setName: " + JwtUtils.getUserNameByToken(usertoken) + value);
        return userMapper.updateUserName(userId, value);
    }
    // 密码
//    @RequestMapping("/pswd")
//    int setPswd(String usertoken, String value) {
//        int userId = JwtUtils.getUserIdByToken(usertoken);
//        System.out.println("/usertoken/setPswd: " + JwtUtils.getUserNameByToken(usertoken) + "修改了Pswd");
//        return userMapper.updateUserPswd(userId,value);
//    };


    // 头像
    @RequestMapping("/img")
    int setImg(String usertoken, String value) {
        int userId = JwtUtils.getUserIdByToken(usertoken);
        System.out.println("/usertoken/setImg: " + JwtUtils.getUserNameByToken(usertoken) + value);
        return userMapper.updateUserImg(userId, value);
    }

    // 信息
    @RequestMapping("/info")
    int setInfo(String usertoken, String value) {
        int userId = JwtUtils.getUserIdByToken(usertoken);
        System.out.println("/usertoken/setInfo: " + JwtUtils.getUserNameByToken(usertoken) + value);
        return userMapper.updateUserInfo(userId, value);
    }

    // 设置
    @RequestMapping("/setting")
    int setSetting(String usertoken, String value) {
//        int userId = JwtUtils.getUserIdByToken(usertoken);
        int userId = Integer.parseInt(UserThreadLocal.get().get("userId"));

        System.out.println("/user/setSetting: userId" + JwtUtils.getUserNameByToken(usertoken) + value);
        return userMapper.updateUserSetting(userId, value);
    }


}
